package com.worldvision.main.page.home;

import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.worldvision.main.page.abstractPageObject;
import com.worldvision.main.page.member.MyProfilePage;
import com.worldvision.main.reg.RegPage;
import com.worldvision.main.util.Report;
import com.worldvision.main.util.UTIL;

public class HomePage extends abstractPageObject
{//---------------------UI objects-------------------------------------------------------//
	
	
	public static Map<String,String> mapUI = new HashMap<String,String>()
	{
		{
			put("username","Login");
			put("password","Pwd");
			put("submit","submit");
		}
	};
		
	private static final CharSequence PAGE_TITLE = "World Vision India: Sponsor a Child, Education Gift, Donate Charity to Help Disabled Child";
	
	public HomePage(WebDriver driver)
	{
		if(!driver.getTitle().contains(PAGE_TITLE))
		{
			//Report.failMSGandAssert("Home page not loaded");
		}
			
		driver.findElement(By.linkText("Home")).click();
		this.driver = driver;
	}

	public MyProfilePage checkAndLogin(String username,String password)
	{
		if(UTIL.isElementPresent(driver, By.name(mapUI.get("username"))))
		{
			driver.findElement(By.name(mapUI.get("username"))).sendKeys(username);
			driver.findElement(By.name(mapUI.get("password"))).clear();
			driver.findElement(By.name(mapUI.get("password"))).sendKeys(password);
			
			//if(UTIL.isElementPresent(By.name(mapUI.get("submit"))))
			//{
			
				driver.findElement(By.name(mapUI.get("submit"))).sendKeys(Keys.ENTER);
			//}
			//else
			//{
				//driver.
			//}
			ExpectedCondition<Boolean> waitForLoginJS = new ExpectedCondition<Boolean>() 
			        {
			            @Override
			            public Boolean apply(WebDriver driver) 
			            {
			               // if(driver.getTitle().contains("World Vision India: My Sponsorship"))
			            	if(driver.findElement(By.linkText("Home")).isDisplayed())
			                	return true;
			                return false;
			            }
			        };
			
			// TODO Auto-generated method stub
			WebDriverWait wait = new WebDriverWait(driver, 20);
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			wait.until(waitForLoginJS);
			
		}
		else
			driver.findElement(By.linkText("My Sponsorship")).click();
		return (new MyProfilePage(driver));
	}
	public MyProfilePage Login(String username, String password) 
	{
		
		
		driver.findElement(By.name(mapUI.get("username"))).sendKeys(username);
		driver.findElement(By.name(mapUI.get("password"))).clear();
		driver.findElement(By.name(mapUI.get("password"))).sendKeys(password);
		
		//if(UTIL.isElementPresent(By.name(mapUI.get("submit"))))
		//{
		
			driver.findElement(By.name(mapUI.get("submit"))).sendKeys(Keys.ENTER);
		//}
		//else
		//{
			//driver.
		//}
		ExpectedCondition<Boolean> waitForLoginJS = new ExpectedCondition<Boolean>() 
		        {
		            @Override
		            public Boolean apply(WebDriver driver) 
		            {
		               // if(driver.getTitle().contains("World Vision India: My Sponsorship"))
		            	if(driver.findElement(By.linkText("Home")).isDisplayed())
		                	return true;
		                return false;
		            }
		        };
		
		// TODO Auto-generated method stub
		WebDriverWait wait = new WebDriverWait(driver, 20);
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		wait.until(waitForLoginJS);
		
		return new MyProfilePage(driver);
	}

	public void checkErrorMessage(String string) {
		// TODO Auto-generated method stub
		 if(driver.getPageSource().contains(string))
		 {
			 System.out.println("Found the expected text "+string);
		 }
		 else
			 System.out.println("Not found");
	}
	
	public void Login1(String username,String password)
	{
		driver.findElement(By.name(mapUI.get("username"))).sendKeys(username);
		driver.findElement(By.name(mapUI.get("password"))).clear();
		driver.findElement(By.name(mapUI.get("password"))).sendKeys(password);
		driver.findElement(By.name(mapUI.get("submit"))).sendKeys(Keys.ENTER);
	}

	public RegPage gotoRegister() {
		// TODO Auto-generated method stub
		driver.findElement(By.linkText("Register")).sendKeys(Keys.ENTER);
		return (new RegPage());
	}

	public void gotoHomePage() {
		// TODO Auto-generated method stub
		driver.findElement(By.linkText("Home")).click();
	}
}
